#coding:utf8
#
# Author: CMGS
# Created on 2011-1-20
#

import sys
# pylibmc is first choice
try:
    from pylibmc import Client
except ImportError:
    from memcache import Client
from threading import local
from common import config as _conf

class Memcached(local):
    def __init__(self, servers = _conf.MEMCACHED_SERVERS):
        if not sys.modules.has_key('memcache'):
            self._mc = Client(servers, binary = True)
            self._mc.behaviors = {"tcp_nodelay": True, "ketama": True}
        else:
            self._mc = Client(servers)
        for item in dir(self._mc):
            if item.startswith('__') or item == 'behaviours':
                continue
            self.__dict__[item] = getattr(self._mc, item)

memcached = Memcached()

if __name__ == '__main__':
    print memcached.set('test', {'123':'abc'})
    print memcached.get('test')
